#include "user_del_dialog.h"
#include "com/usermgr/usermgr.h"
#include "com/sysmgr/sysmgr.h"

// 前向声明
static void btn_ok_event_cb(lv_event_t* e);
static void btn_cancel_event_cb(lv_event_t* e);
static void update_user_list(lv_obj_t* dd);

// 创建删除用户对话框
user_del_dialog_t* user_del_dialog_create(lv_obj_t* parent, void (*close_cb)(void)) {
    // 分配内存
    user_del_dialog_t* dialog = (user_del_dialog_t*)lv_mem_alloc(sizeof(user_del_dialog_t));
    if (dialog == NULL) {
        return NULL;
    }
    lv_memset(dialog, 0, sizeof(user_del_dialog_t));

    dialog->close_cb = close_cb;

    // 创建对话框
    dialog->obj = lv_obj_create(parent);
    lv_obj_set_size(dialog->obj, 340, 200);
    lv_obj_center(dialog->obj);
    lv_obj_set_style_pad_all(dialog->obj, 20, 0);

    // 创建标题
    lv_obj_t* title = lv_label_create(dialog->obj);
    lv_label_set_text(title, "Delete User");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 0);

    // 创建用户选择区域的容器
    lv_obj_t* user_cont = lv_obj_create(dialog->obj);
    lv_obj_set_size(user_cont, 300, 40);
    lv_obj_align(user_cont, LV_ALIGN_TOP_LEFT, 0, 50);
    lv_obj_set_style_pad_all(user_cont, 0, 0);
    lv_obj_set_style_border_width(user_cont, 0, 0);
    lv_obj_set_flex_flow(user_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(user_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建用户选择标签和下拉框
    lv_obj_t* user_label = lv_label_create(user_cont);
    lv_label_set_text(user_label, "Select User:");
    lv_obj_set_style_min_width(user_label, 80, 0);

    dialog->user_dd = lv_dropdown_create(user_cont);
    lv_obj_set_size(dialog->user_dd, 180, 40);
    update_user_list(dialog->user_dd);

    // 创建按钮容器
    lv_obj_t* btn_container = lv_obj_create(dialog->obj);
    lv_obj_set_size(btn_container, 260, 50);
    lv_obj_align(btn_container, LV_ALIGN_BOTTOM_MID, 0, -10);
    lv_obj_set_flex_flow(btn_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(btn_container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_all(btn_container, 0, 0);
    lv_obj_set_style_border_width(btn_container, 0, 0);

    // 创建确认按钮
    lv_obj_t* btn_ok = lv_btn_create(btn_container);
    lv_obj_set_size(btn_ok, 120, 40);
    lv_obj_add_event_cb(btn_ok, btn_ok_event_cb, LV_EVENT_CLICKED, dialog);
    lv_obj_t* label_ok = lv_label_create(btn_ok);
    lv_label_set_text(label_ok, "OK");
    lv_obj_center(label_ok);

    // 创建取消按钮
    lv_obj_t* btn_cancel = lv_btn_create(btn_container);
    lv_obj_set_size(btn_cancel, 120, 40);
    lv_obj_add_event_cb(btn_cancel, btn_cancel_event_cb, LV_EVENT_CLICKED, dialog);
    lv_obj_t* label_cancel = lv_label_create(btn_cancel);
    lv_label_set_text(label_cancel, "Cancel");
    lv_obj_center(label_cancel);

    return dialog;
}

// 更新用户列表
static void update_user_list(lv_obj_t* dd) {
    UserInfo* users = NULL;
    int count = 0;
    if (usermgr_get_all_users(&users, &count) == 0) {
        char* options = (char*)lv_mem_alloc(count * 32);
        if (options) {
            options[0] = '\0';
            int i = 0;
            for (i = 0; i < count; i++) {
                strcat(options, users[i].username);
                if (i < count - 1) {
                    strcat(options, "\n");
                }
            }
            lv_dropdown_set_options(dd, options);
            lv_mem_free(options);
        }
        usermgr_free_user_list(users);
    }
}

// 确认按钮事件回调
static void btn_ok_event_cb(lv_event_t* e) {
    user_del_dialog_t* dialog = (user_del_dialog_t*)lv_event_get_user_data(e);
    if (!dialog) return;

    char username[32];
    lv_dropdown_get_selected_str(dialog->user_dd, username, sizeof(username));

    // 验证是否为admin用户
    if (strcmp(username, "admin") == 0) {
        lv_obj_t* mbox = lv_msgbox_create(NULL, "Error", "Cannot delete admin user", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    // 验证是否为当前用户
    const char* current_user = sysmgr_get_username();
    if (strcmp(username, current_user) == 0) {
        lv_obj_t* mbox = lv_msgbox_create(NULL, "Error", "Cannot delete current user", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    // 删除用户，使用当前登录用户的凭据作为操作者
    const char* operator_username = sysmgr_get_username();
    const char* operator_password = sysmgr_get_password();
    if (usermgr_delete_user(username, operator_username, operator_password)) {
        // 关闭对话框
        if (dialog->close_cb) {
            dialog->close_cb();
        }
        lv_obj_del(dialog->obj);
        lv_mem_free(dialog);
    } else {
        lv_obj_t* mbox = lv_msgbox_create(NULL, "Error", "Failed to delete user", NULL, true);
        lv_obj_center(mbox);
    }
}

// 取消按钮事件回调
static void btn_cancel_event_cb(lv_event_t* e) {
    user_del_dialog_t* dialog = (user_del_dialog_t*)lv_event_get_user_data(e);
    if (!dialog) return;

    // 关闭对话框
    if (dialog->close_cb) {
        dialog->close_cb();
    }
    lv_obj_del(dialog->obj);
    lv_mem_free(dialog);
}